home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 151-175 / disk_172 / spiff / line.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  4KB  |  170 lines

  1. /*                        Copyright (c) 1988 Bellcore
  2. **                            All Rights Reserved
  3. **       Permission is granted to copy or use this program, EXCEPT that it
  4. **       may not be sold for profit, the copyright notice must be reproduced
  5. **       on copies, and credit should be given to Bellcore where it is due.
  6. **       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7. */
  8.  
  9.  
  10. #ifndef lint
  11. static char rcsid[]= "$Header: line.c,v 1.1 88/09/15 11:34:00 daniel Rel $";
  12. #endif
  13.  
  14. #include <stdio.h>
  15. #include "misc.h"
  16. #include "token.h"
  17. #include "line.h"
  18.  
  19. char *_L_al[_L_MAXLINES];  /* storage for lines */
  20. char *_L_bl[_L_MAXLINES];
  21.  
  22. int   _L_ai[_L_MAXLINES];  /* index from token line number to first token */
  23. int   _L_bi[_L_MAXLINES];
  24.  
  25. int   _L_ac[_L_MAXLINES];  /* count of tokens on this token line */
  26. int   _L_bc[_L_MAXLINES];
  27.  
  28. int   _L_arlm;        /* count of real lines in the file */
  29. int   _L_brlm;
  30.  
  31. int   _L_aclm;        /* count of content lines in the file */
  32. int   _L_bclm;
  33.  
  34. int   _L_atlm;        /* count of token lines in the file */
  35. int   _L_btlm;
  36.  
  37. int   _L_aclindex[_L_MAXLINES]; /* mapping from content lines to real lines*/
  38. int   _L_bclindex[_L_MAXLINES]; 
  39.  
  40. int   _L_atlindex[_L_MAXLINES]; /*mapping from token lines to content lines */
  41. int   _L_btlindex[_L_MAXLINES];
  42.  
  43.  
  44. static void
  45. _L_setrline(file,X,str)
  46. int file;
  47. int X;
  48. char *str;
  49. {
  50.     if (file)
  51.     {
  52.         S_savestr(&_L_bl[X],str);
  53.     }
  54.     else
  55.     {
  56.         S_savestr(&_L_al[X],str);
  57.     }
  58.     return;
  59. }
  60. /*
  61. **    returns 1 if we reached the end of file
  62. **    returns 0 if there is more to do
  63. **
  64. **    stores data and sets maximum counts
  65. */
  66. L_init_file(fnumber,fname)
  67. int fnumber;
  68. char *fname;
  69. {
  70.     extern char *fgets();
  71.     FILE *fp;
  72.     static char buf[Z_LINELEN+2];    /* +2 is to leave room for us to add
  73.                         a newline if we need to */
  74.     int ret_val = 1;
  75.     int tmplen;
  76.  
  77.     if ((fp = fopen(fname,"r")) == (FILE*) NULL)
  78.     {
  79.         (void) sprintf(Z_err_buf, "Cannot open file %s.\n",fname);
  80.         Z_fatal(Z_err_buf);
  81.     }
  82.  
  83.     /*
  84.     **    clear the line count
  85.     */
  86.     _L_setrlmx(fnumber,0);
  87.  
  88.     /*
  89.     **    read in the entire file
  90.     */
  91.     while (fgets(buf,Z_LINELEN+1,fp) != (char *) NULL)
  92.     {
  93.         tmplen = strlen(buf);
  94.         if (tmplen <= 0)
  95.         {
  96.             (void) sprintf(Z_err_buf,
  97.               "fatal error -- got 0 length line %d in file %s\n",
  98.                 L_getrlmax(fnumber)+1,
  99.                 fname);
  100.             Z_fatal(Z_err_buf);
  101.         }
  102.         else if (tmplen > Z_LINELEN)
  103.         {
  104.             (void) sprintf(Z_err_buf,
  105.   "got fatally long line %d in file %s length is %d, must be a bug\n",
  106.                 L_getrlmax(fnumber)+1,
  107.                 fname,tmplen);
  108.             Z_fatal(Z_err_buf);
  109.         }
  110.         /*
  111.         **    look for newline as last character
  112.         */
  113.         if ('\n' != buf[tmplen-1])
  114.         {
  115.             /*
  116.             **    did we run out room in the buffer?
  117.             */
  118.             if (tmplen == Z_LINELEN)
  119.             {
  120.             (void) sprintf(Z_err_buf,
  121.     "line %d too long in file %s, newline added after %d characters\n",
  122.                 L_getrlmax(fnumber)+1,
  123.                 fname,Z_LINELEN);
  124.             Z_complain(Z_err_buf);
  125.             }
  126.             else
  127.             {
  128.             (void) sprintf(Z_err_buf,
  129.     "didn't find a newline at end of line %d in file %s, added one\n",
  130.                 L_getrlmax(fnumber)+1,
  131.                 fname);
  132.             Z_complain(Z_err_buf);
  133.             }
  134.  
  135.             buf[tmplen] = '\n';
  136.             buf[tmplen+1] = '\0';
  137.         }
  138.  
  139.         _L_setrline(fnumber,L_getrlmax(fnumber),buf);
  140.  
  141.         if (L_getrlmax(fnumber) >= _L_MAXLINES-1)
  142.         {
  143.             (void) sprintf(Z_err_buf,
  144.     "warning -- ran out of space reading %s, truncated to %d lines\n",
  145.                 fname,_L_MAXLINES);
  146.             Z_complain(Z_err_buf);
  147.             ret_val= 0;
  148.             break;
  149.         }
  150.         else
  151.         {
  152.             /*
  153.             **    increment the line count
  154.             */
  155.             _L_incrlmx(fnumber);
  156.         }
  157.  
  158.     }
  159.  
  160.     (void) fclose(fp);
  161.     /*
  162.     **    reset line numbers
  163.     */
  164.     L_setclmax(fnumber,0);
  165.     L_settlmax(fnumber,0);
  166.  
  167.     return(ret_val);
  168. }
  169.  
  170.